import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Index from '../views/index/index.vue'
import Layout from '@/layout/index.vue'
import { start, close } from '@/utils/nprogress';

const routes: Array<RouteRecordRaw> = [
  {
    // 首页
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        component: Index,
        meta: {
          title: '首页 - 云音乐'
        }
      }
    ]
  },
  {
    // 首页二级菜单
    path: '/discover',
    component: Layout,
    children: [
      {
        path: '',
        component: Index,
        meta: {
          title: '首页 - 云音乐'
        }
      },
      {
        // 排行榜
        path: 'toplist',
        component: () => import('@/views/discover/top-list/index.vue'),
        name: 'toplist',
        meta: {
          title: '榜单 - 云音乐'
        }
      },
      {
        // 歌单
        path: 'playlist',
        component: () => import('@/views/discover/play-list/index.vue'),
        name: 'playlist',
        meta: {
          title: '歌单 - 云音乐'
        }
      },
      {
        // 歌手
        path: 'artist',
        component: () => import('@/views/discover/artist/index.vue'),
        name: 'artist',
        meta: {
          title: '歌手 - 云音乐'
        }
      },
      {
        // 歌手
        path: 'album',
        component: () => import('@/views/discover/album/index.vue'),
        name: 'album',
        meta: {
          title: '专辑 - 云音乐'
        }
      },
      {
        // 歌曲详情页
        path: 'song/:id',
        component: () => import('@/views/song/index.vue'),
        name: 'song',
        meta: {
          title: '歌曲详情页 - 云音乐'
        }
      },
      {
        // 歌曲详情页
        path: 'play-list-detail/:id',
        component: () => import('@/views/play-list-detail/index.vue'),
        name: 'play-list-detail',
        meta: {
          title: '歌单详情页 - 云音乐'
        }
      },
      {
        // 专辑详情页
        path: 'album-detail/:id',
        component: () => import('@/views/album-detail/index.vue'),
        name: 'album-detail',
        meta: {
          title: '专辑详情页 - 云音乐'
        }
      },
      {
        // 艺人 / 歌手 详情页
        path: 'artist-detail/:id',
        component: () => import('@/views/artist-detail/index.vue'),
        name: 'artist-detail',
        meta: {
          title: '歌手详情页 - 云音乐'
        }
      },
      {
        // 艺人 / 歌手 详情页
        path: 'mvs-detail/:id',
        component: () => import('@/views/mvs-detail/index.vue'),
        name: 'mvs-detail',
        meta: {
          title: 'MV详情页 - 云音乐'
        }
      }
    ]
  },
  {
    // 我的音乐
    path: '/my-music',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/my-music/index.vue'),
        name: 'my-music',
        meta: {
          title: '我的音乐 - 云音乐'
        }
      }
    ]
  },
  {
    // 关注
    path: '/friend',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/friend/index.vue'),
        name: 'friend',
        meta: {
          title: '关注 - 云音乐'
        }
      }
    ]
  },
  {
    // 用户个人中心页
    path: '/user-home/:id',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/user-home/index.vue'),
        name: 'user-home',
        meta: {
          title: '用户个人中心页 - 云音乐'
        }
      }
    ]
  },
  {
    // 搜索
    path: '/search/:s',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/search/index.vue'),
        name: 'search',
        meta: {
          title: '搜索 - 云音乐'
        }
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})
router.beforeEach((to, from, next) => {
  start(); //---------------------------------------------->此处开始
  /* 路由发生变化修改页面title */
  if (typeof (to.meta.title) === 'string') {
    document.title = to.meta.title
  }
  // 路由出错处理
  // if (to.matched.length === 0 && !to.name) {
  //     ElNotification({
  //     title: 'Error',
  //     message: 'This is an error message',
  //     type: 'error',
  //     duration: 5000,
  //     });
  //     ElMessage.error('404');
  //     router.push('/404');
  // } else if (to.path !== '/login' && to.path !== '/404') {
  //     if (idUser.value === -1) {
  //     console.log('未登录');
  //     ElNotification({
  //         title: '未登录状态',
  //         message: '您当前未登录，请登录后再访问！',
  //         type: 'error',
  //         duration: 5000,
  //     });
  //     ElMessage.error('您当前未登录，请登录后再访问！');
  //     router.push('/login');
  //     }
  // }
  next()
})
// 路由加载结束后执行
router.afterEach(() => {

  close();//---------------------------------------------->此处结束
});

export default router
